package com.xjwlmq.dx.servlet;

import com.alibaba.fastjson.JSON;
import com.xjwlmq.dx.dao.impl.RainQualityImpl;
import com.xjwlmq.dx.entity.RainQuality;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName RainQualityAddServlet
 * @Description TODO:
 * @Author 12994
 * @Date 2024/9/30 12:09
 * @Version 1.0
 **/
@WebServlet("/addRQ")
public class RainQualityAddServlet extends HttpServlet {
    RainQualityImpl rainQuality = new RainQualityImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // super.service(req, resp);
        // 设置响应编码格式
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html:charset=UTF-8");
        /*
         * 添加一条记录
         * req.getParameter();从get url中提取参数
         * Integer.getInteger; 从字符串转Int
         */
        RainQuality thisRainQuality = new RainQuality();
        thisRainQuality.setDistrictName(req.getParameter("DistrictName"));
        thisRainQuality.setRain(Integer.parseInt(req.getParameter("Rain")));
        thisRainQuality.setStationName(req.getParameter("MonitoringStation"));
        thisRainQuality.setStationType(Integer.parseInt(req.getParameter("StationType")));
        thisRainQuality.setMonitoringAddress(req.getParameter("MonitoringAddress"));
        thisRainQuality.setMonitorTime(req.getParameter("MonitorTime"));

        int cont = rainQuality.addRainQuality(thisRainQuality);
        if (cont > 0) {
            resp.getWriter().write(JSON.toJSONString("成功添加了%d行数据.".formatted(cont)));
        } else {
            resp.getWriter().write(JSON.toJSONString("没有影响数据..."));
        }
    }
}
